/*
 * marketsim-eclipsetrader
 *
 * Copyright (C) 2008-2011 Steve Phelps and John Cartlidge
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 */
package victria.marketsim.eclipsetrader.feed;

import java.util.Currency;
import java.util.Date;

import org.apache.log4j.Logger;

import net.sourceforge.eclipsetrader.core.IFeed;
import net.sourceforge.eclipsetrader.core.db.Security;
import net.sourceforge.eclipsetrader.core.db.feed.Quote;

public class QuoteFeed extends AbstractFeed implements IFeed {

	protected Currency currency = Currency.getInstance("GBP");
	
	static Logger logger = Logger.getLogger(QuoteFeed.class);

	public void refresh() {
		logger.debug("Refreshing..");
		for(Security security:  securities) {
			security.setCurrency(currency);
			Date date = new Date(marketService.currentTimeMillis());
			double bid = marketService.getMarketState().getSpread().getBidPrice();
			double ask = marketService.getMarketState().getSpread().getAskPrice();
			double last = marketService.getLastTransactionPrice();
			Quote quote = new Quote(date, last, bid, ask);
			logger.debug("Quote for " + security + ": " + quote);
			security.setQuote(quote, new Double(0), new Double(1000), new Double(0), new Double(0));
		}
	}
	
	public void setCurrency(String currencyCode) {
		currency = Currency.getInstance(currencyCode);
	}
	
	public String getCurrency() {
		return currency.getCurrencyCode();
	}

	@Override
	public void snapshot() {
		refresh();
	}
}
